package com.roc.web.action.agent;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.SerialNumBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.AuditingItem;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.ITerminalService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.service.agent.IAgentInfoService;
import com.roc.service.operator.IPowerInfoService;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigMerinfoUtil;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Terminal;


@Component
@Scope("prototype")
@Namespace("/ysbSerialNumQuery")
public class YsbSerialNumAction extends PaginationBaseAction{
	
	private Logger logger=LogManager.getLogger(YsbSerialNumAction.class);
	
	
	private YsbSerialNumQueryCondition serialNumCondition;
	
	private Operators merchant;
	
	private AuditingItem auditing;
	
	private List<OrgBean> orgList=new ArrayList<OrgBean>();
	
	private List<Operators> operatorsList=new ArrayList<Operators>();
	
	private List<String> terList;
	
	public List<String> getTerList() {
		return terList;
	}

	public void setTerList(List<String> terList) {
		this.terList = terList;
	}

	private String code;
	
	@Autowired
	IorgService orgService;
	
	@Autowired
	IPageService pagenitionService;
	
	@Autowired 
	IMerchantServices merchantServices;
	
	@Autowired
	IPowerInfoService powerInfoService;
	
	@Autowired
	IAgentInfoService agentInfoService;
	
	@Autowired
	ITerminalService terminalService;
	
	@Action(value = "SerialNumQuery", results = { @Result(name = "success", location = "/pages/agent/ysbSerialNumQuery.jsp")
			,@Result(name = "error", location = "/error.jsp") })
	//查询机器序列号
	public String SerialNumQuery() {
		
        Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
        long id = logingMerchant.getId();
               
        Map<String,String> brandMap=ConfigProperties.getYsbBrandProperties();
		Map<String,String> nameMap=new HashMap<String, String>();
		Map<String,String> typeMap= ConfigProperties.getSysDB1("Terminal_Type",true);
        
		if(serialNumCondition==null){
			serialNumCondition=(YsbSerialNumQueryCondition)getSearchConditionFromSession();
			if(serialNumCondition==null){
				serialNumCondition=new YsbSerialNumQueryCondition();
			}
		}
		try {
			serialNumCondition.setAgentId(id);
			int rows=pagenitionService.countSQL(serialNumCondition);
			searchRsList=pagenitionService.findAllSQL(serialNumCondition, calcuStart(getCurrentPage()),Constance.PAGE_MAX_SIZE);
			List<SerialNumBean> dealList = new ArrayList<SerialNumBean>();
			
			if(searchRsList!=null && searchRsList.size()>0){
				Iterator iterator=searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj= (Object[])iterator.next();
					SerialNumBean ser = new SerialNumBean();
					ser.setSerialNum(Constance.Null2EmptyTrim(obj[0]));
					ser.setPosBrand(brandMap.get(Constance.Null2EmptyTrim(obj[1])));
					ser.setPosType(typeMap.get(Constance.Null2EmptyTrim(obj[2])));
					//ser.setPosName(nameMap.get(Constance.Null2EmptyTrim(obj[3])));				
					ser.setPosName(ConfigMerinfoUtil.getPosMODEL(Constance.Null2EmptyTrim(obj[3]), Constance.Null2EmptyTrim(obj[1])));
					ser.setIsUsed(Terminal.valueOfCode(Constance.Null2EmptyTrim(obj[4])).getName());
					ser.setDtCreate(Constance.Null2EmptyTrim(obj[5]));
					ser.setDtLastmod(Constance.Null2EmptyTrim(obj[6]));
					
					// 厂商编号
					ser.setPosBrandCode(Constance.Null2EmptyTrim(obj[1]));
					dealList.add(ser);
				}
			}
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute("yjPosAgentlist",dealList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			getRequest().setAttribute("selModelCode", serialNumCondition.getPosName());
			getRequest().setAttribute("oldBandCode", serialNumCondition.getPosBrand());
			getRequest().setAttribute("brandMap",brandMap);
			getRequest().setAttribute("nameMap", nameMap);
			getRequest().setAttribute("typeMap", typeMap);
			getRequest().setAttribute("roleType", logingMerchant.getRoleType());
			
			getRequest().setAttribute("code",code);
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			addActionError(e.getMessage());
			return ERROR;
		}
		
		return SUCCESS;
	}
	
	@Action(value = "SerialNumTer", results = 
		{ @Result(name = "success",type="chain", location = "SerialNumQuery")
			,@Result(name = "error", location = "/error.jsp") })
	//查询代理商
	public String SerialNumTer() {
		 String context = "";//提示信息
		 Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		String str=terList.get(0);
		System.out.println("str===="+str);
		 if(terList.size()<1){
			 addActionError("最少选择一个终端");
			 return ERROR;
		 }
		 String flag = null;
		 if(op.getRoleType().equals("14")){
			flag= serialNumCondition.getFlag();
			if(flag.equals("")||flag.equals(null)){
				 addActionError("请选择终端等级");
				 return ERROR;
		    }
		 }
		try {
			Operators operator = powerInfoService.loadOperatorsById(new Long(code));//分批终端序列号目的对象
			int terCount = terminalService.ysbSerialNumTer(terList,op,code,flag);
			context = "操作员【" + op.getAccountNo() + "】成功分配【"+ terCount+ "】个终端序列号给用户"+"【"+operator.getAccountNo()+"】";
		//	addActionError(context);
			getRequest().setAttribute("msg", context);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return ERROR;
		}
		return SerialNumQuery();
	}
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return "200";
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (YsbSerialNumQueryCondition)getSession().getAttribute(SEARCH_SESSION_SERIAL_NUM_QUERY);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return serialNumCondition;
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(serialNumCondition);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_SERIAL_NUM_QUERY, searchCondition);
	}

	public Operators getMerchant() {
		return merchant;
	}

	public void setMerchant(Operators merchant) {
		this.merchant = merchant;
	}


	
	public YsbSerialNumQueryCondition getSerialNumCondition() {
		return serialNumCondition;
	}

	public void setSerialNumCondition(YsbSerialNumQueryCondition serialNumCondition) {
		this.serialNumCondition = serialNumCondition;
	}

	public AuditingItem getAuditing() {
		return auditing;
	}

	public void setAuditing(AuditingItem auditing) {
		this.auditing = auditing;
	}

	public List<OrgBean> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<OrgBean> orgList) {
		this.orgList = orgList;
	}

	public List<Operators> getOperatorsList() {
		return operatorsList;
	}

	public void setOperatorsList(List<Operators> operatorsList) {
		this.operatorsList = operatorsList;
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}
